/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dp1.titandevelop.titano.view.prod;

import dp1.titandevelop.titano.bean.EmpleadoEficiencia;
import dp1.titandevelop.titano.bean.Mensajes;
import dp1.titandevelop.titano.persistent.Empleado;
import dp1.titandevelop.titano.persistent.EmpleadoXProcesoTurno;
import dp1.titandevelop.titano.persistent.Maquina;
import dp1.titandevelop.titano.persistent.Zona;
import dp1.titandevelop.titano.service.EmpleadoService;
import dp1.titandevelop.titano.service.EmpleadoXProcesoTurnoService;
import dp1.titandevelop.titano.service.MaquinaService;
import dp1.titandevelop.titano.service.ProcesoService;
import dp1.titandevelop.titano.service.ValidacionService;
import dp1.titandevelop.titano.service.ZonaService;
import java.awt.Cursor;
import java.awt.Dimension;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;

/**
 *
 * @author Yulian
 */
public class ModulosT_PRO_ProcProdNuevo extends javax.swing.JInternalFrame {
    private  List<Zona> zonas;
    private ArrayList<EmpleadoXProcesoTurno> listaEmpleadoTotales; 
    private ArrayList<EmpleadoEficiencia> listaEmpleadosBD;   
    
    /**
     * Creates new form ModulosT_ALM_TMovNuevo
     */
    private ArrayList<EmpleadoEficiencia> hallarListaEmpleado() {
           EmpleadoService empleadoservice = new EmpleadoService();
           ArrayList<Empleado> empleados = (ArrayList) empleadoservice.buscarTOperario();
           EmpleadoXProcesoTurnoService empleadoProcesoService = new EmpleadoXProcesoTurnoService();
           this.listaEmpleadoTotales=empleadoProcesoService.buscarAll();
           ArrayList<EmpleadoEficiencia> aux=new ArrayList();
           for(int i=0;i<empleados.size();i++){
               EmpleadoEficiencia e=new EmpleadoEficiencia();
               e.setE(empleados.get(i));
               e.setEficienciaM(buscarData(empleados.get(i).getIdempleado(),1));
               e.setEficienciaT(buscarData(empleados.get(i).getIdempleado(),2));
               e.setEficienciaN(buscarData(empleados.get(i).getIdempleado(),3));
               aux.add(e);
           }
           
          return aux;
    }
    private float buscarData(int idempleado, int idTurno) {
        float total=0;
        float eficienciaParcial=0;
        float eficiencia=0;
        int cantidad=0;
                
        for(int i=0;i<listaEmpleadoTotales.size();i++){
            if(listaEmpleadoTotales.get(i).getToEmpleado().getIdempleado().equals(idempleado)
                    && listaEmpleadoTotales.get(i).getToTurno().getIdturno().equals(idTurno)){
            total=listaEmpleadoTotales.get(i).getProductividad()+listaEmpleadoTotales.get(i).getRotura();
            eficienciaParcial+=listaEmpleadoTotales.get(i).getProductividad()/total;
            cantidad++;
            }
        }
        
        eficiencia=eficienciaParcial/cantidad;
       
      
        return eficiencia; 
        }
    private void addList (List<Zona> lista){
        
        for(int i=0;i<lista.size();i++){
            this.cmbzona.addItem(lista.get(i).getDescripcion());
        }
    } 
     
    public ModulosT_PRO_ProcProdNuevo() {
       initComponents();
               
        ProcesoService serv = new ProcesoService();
        ValidacionService val = new ValidacionService();
        zonas = serv.buscarZonaAll();
        val.SNumeros(txtCant);
        val.SSoloLetra(this.txtDescripcion);
        ZonaService se = new ZonaService();
        zonas = se.buscar();//Obtengos las zonas de la bd
        addList(zonas);
        val.SNumeros(txtproduct);
        val.SNumeros(txtrotura);
        listaEmpleadosBD = hallarListaEmpleado();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        txtDescripcion = new javax.swing.JTextField();
        txtCant = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        cmbzona = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        btnGuardar = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        txtproduct = new javax.swing.JTextField();
        txtrotura = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();

        setResizable(true);
        setTitle("Nuevo Proceso");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Datos"));

        jLabel1.setText("Descripción");

        jLabel2.setText("Cant. Maquina");

        cmbzona.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmbzonaActionPerformed(evt);
            }
        });

        jLabel3.setText("Zona");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(txtDescripcion)
                    .addComponent(txtCant)
                    .addComponent(cmbzona, 0, 136, Short.MAX_VALUE))
                .addContainerGap(42, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtDescripcion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtCant))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cmbzona, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addContainerGap(15, Short.MAX_VALUE))
        );

        btnGuardar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/grabar.png"))); // NOI18N
        btnGuardar.setToolTipText("Guardar");
        btnGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnGuardarActionPerformed(evt);
            }
        });

        btnCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/cancelar.png"))); // NOI18N
        btnCancelar.setToolTipText("Cancelar");
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Promedios para Empleados"));

        jLabel4.setText("Productividad");

        jLabel5.setText("Rutura");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4)
                    .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(txtproduct, javax.swing.GroupLayout.DEFAULT_SIZE, 136, Short.MAX_VALUE)
                    .addComponent(txtrotura))
                .addContainerGap(42, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(txtproduct, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtrotura))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(btnGuardar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnCancelar))
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(10, 10, 10)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnCancelar, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(btnGuardar, javax.swing.GroupLayout.Alignment.TRAILING))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
   
    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        ModulosT_PRO.menuProceso = new ModulosT_PRO_ProcProd();
        this.getParent().add( ModulosT_PRO.menuProceso);
        
        Dimension desktopSize = this.getParent().getSize();
        Dimension jInternalFrameSize =  ModulosT_PRO.menuProceso.getSize();
         ModulosT_PRO.menuProceso.setLocation((desktopSize.width - jInternalFrameSize.width) / 2,
                (desktopSize.height - jInternalFrameSize.height)/4 );        
         ModulosT_PRO.menuProceso.show();
        this.dispose();
    }//GEN-LAST:event_btnCancelarActionPerformed

    private void btnGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGuardarActionPerformed
       
       ValidacionService val = new ValidacionService();    
        Mensajes m = new Mensajes();
        if (val.esVacio(txtCant)|| val.esVacio(this.txtDescripcion)){
            JOptionPane.showInternalMessageDialog(this.getRootPane(), m.getCompletarCampos(), "Aviso", JOptionPane.WARNING_MESSAGE);

        }else{ 
       //  try{
            ProcesoService tp = new ProcesoService();
            int prod = Integer.parseInt(txtproduct.getText());
            int merma = Integer.parseInt(txtrotura.getText());
            this.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
            tp.insertarProceso(zonas.get(cmbzona.getSelectedIndex()).getIdzona(),this.txtDescripcion.getText(),this.txtCant.getText(),listaEmpleadosBD, prod, merma);     
            this.setCursor(Cursor.getDefaultCursor());

//            tm.insertar();
            JOptionPane.showInternalMessageDialog(this.getRootPane(),m.getRegistrado(),"Mensaje",JOptionPane.INFORMATION_MESSAGE);

            ModulosT_PRO.menuProceso = new ModulosT_PRO_ProcProd();
            this.getParent().add( ModulosT_PRO.menuProceso);
            Dimension desktopSize = this.getParent().getSize();
            Dimension jInternalFrameSize =  ModulosT_PRO.menuProceso.getSize();
            ModulosT_PRO.menuProceso.setLocation((desktopSize.width - jInternalFrameSize.width) / 2,
                    (desktopSize.height - jInternalFrameSize.height)/4 );
            ModulosT_PRO.menuProceso.show();

            this.dispose();
//            } catch(Exception e) {
//                              JOptionPane.showInternalMessageDialog(this.getRootPane(),m.getError(),"Error",JOptionPane.ERROR_MESSAGE);
//                      
//            }
            }
    }//GEN-LAST:event_btnGuardarActionPerformed

    private void cmbzonaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmbzonaActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_cmbzonaActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnGuardar;
    private javax.swing.JComboBox cmbzona;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JTextField txtCant;
    private javax.swing.JTextField txtDescripcion;
    private javax.swing.JTextField txtproduct;
    private javax.swing.JTextField txtrotura;
    // End of variables declaration//GEN-END:variables
}
